package com.lilihub.miaosha.service;

import com.alibaba.fastjson.JSON;
import com.lilihub.miaosha.domain.Goods;
import com.lilihub.miaosha.redis.KeyPrefix;
import com.lilihub.miaosha.vo.GoodsVo;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;


/**
 * @BelongsProject: miaosha
 * @BelongsPackage: com.lilihub.miaosha.service
 * @Author: LiYing
 * @CreateTime: 2021-04-22 12:59
 * @Description:
 */
@Service
public class RedisService {

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private OrderService orderService;


    /**
     * 获取单个对象
     * @param prefix
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T get(KeyPrefix prefix, String key, Class<T> clazz){

        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();

            //生成真正的key
            String realKey = prefix.getPrefix() + key;
            String str = jedis.get(realKey);

            T t = StringToBean(str,clazz);

            return t;

        }finally {
            returnToPool(jedis);
        }
    }

    /**
     * 设置对象
     * @param prefix
     * @param key
     * @param value
     * @param <T>
     * @return
     */
    public <T> boolean set(KeyPrefix prefix,String key,T value){

        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();

            String str = beanToString(value);

            if(str == null || str.length() <= 0){
                return false;
            }

            //生成真正的key
            String realKey = prefix.getPrefix() + key;

            int seconds = prefix.expireSeconds();

            if (seconds <= 0){

                jedis.set(realKey,str);

            }else{

                jedis.setex(realKey,seconds, str);

            }

            return true;

        }finally {
            returnToPool(jedis);
        }
    }

    /**
     * 判断key是否存在
     */
    public <T> boolean exists(KeyPrefix prefix,String key){
        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();

            //生成真正的key
            String realKey = prefix.getPrefix() + key;

            return jedis.exists(realKey);

        }finally {
            returnToPool(jedis);
        }

    }

    /**
     * 删除缓存
     */
    public boolean delete(KeyPrefix prefix,String key){
        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();

            //生成真正的key
            String realKey = prefix.getPrefix() + key;

            Long ret = jedis.del(realKey);

            return ret > 0;

        }finally {
            returnToPool(jedis);
        }

    }

    /**
     * 把bean对象转化为字符串
     * @param value
     * @param <T>
     * @return
     */
    public static  <T> String beanToString(T value) {

        if (value == null){
            return null;
        }

        Class<?> clazz = value.getClass();

        if (clazz == int.class || clazz == Integer.class){
            return "" + value;

        }else if (clazz == String.class){
            return (String) value;

        }else if (clazz == long.class || clazz == Long.class){
            return "" + value;

        }else{

            return JSON.toJSONString(value);
        }
    }


    /**
     * 把字符串转化为bean对象
     * @param str
     * @param clazz
     * @param <T>
     * @return
     */
    public static  <T> T StringToBean(String str,Class<T> clazz) {

        if (str == null || str.length() <= 0 || clazz == null) {
            return null;
        }

        if (clazz == int.class || clazz == Integer.class) {
            return (T) Integer.valueOf(str);

        } else if (clazz == String.class) {
            return (T) str;

        } else if (clazz == long.class || clazz == Long.class) {
            return (T) Long.valueOf(str);

        } else {

            return JSON.toJavaObject(JSON.parseObject(str),clazz);
        }

    }

    /**
     * 增加值   key从122--123
     * @param prefix
     * @param key
     * @param <T>
     * @return
     */
    public <T> Long incr(KeyPrefix prefix,String key){
        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();

            //生成真正的key
            String realKey = prefix.getPrefix() + key;

            return jedis.incr(realKey);

        }finally {
            returnToPool(jedis);
        }

    }


    /**
     * 减少值   key从123--122
     * @param prefix
     * @param key
     * @param <T>
     * @return
     */
    public <T> Long decr(KeyPrefix prefix,String key){
        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();

            //生成真正的key
            String realKey = prefix.getPrefix() + key;

            return jedis.decr(realKey);

        }finally {
            returnToPool(jedis);
        }

    }


    private void returnToPool(Jedis jedis) {
        if (jedis != null){
            jedis.close();
        }
    }


    public void reset(List<GoodsVo> goodsVoList) {

        goodsService.resetStock(goodsVoList);
        orderService.deletOrders();
    }
}
